package com.java.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.business.entity.Course;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.business.dao.CourseCategoryDao;
import com.java.business.entity.CourseCategory;
import com.java.business.service.CourseCategoryService;
import com.java.common.utils.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("courseCategoryService")
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryDao, CourseCategory> implements CourseCategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseCategory> page = this.page(
                new Query<CourseCategory>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<CourseCategory>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveBatchCourseCategory(Course course) {
        LambdaQueryWrapper<CourseCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseCategory::getCourseId,course.getId());
        this.baseMapper.delete(queryWrapper);
        List<CourseCategory> courseCategoryList = course.getCategorys().stream().map(e -> {
            CourseCategory courseCategory = new CourseCategory();
            courseCategory.setId(UuidUtil.getShortUuid());
            courseCategory.setCourseId(course.getId());
            courseCategory.setCategoryId(e.getId());
            return courseCategory;
        }).collect(Collectors.toList());
        this.saveBatch(courseCategoryList);
    }

    @Override
    public List<CourseCategory> listCategory(String courseId) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<CourseCategory>().eq(CourseCategory::getCourseId,courseId));
    }

}